﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.StockManage
{
    public partial class HalfTransferAdd : AuthBasePage
    {
        private string m_strTbl = "BS_HalfTransfer" + DateTime.Now.ToString("yyMMdd");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDL();
                lblHalfTransferID.Text = ClsComm.GetMaxSequenceID(m_strTbl, "HP" + DateTime.Now.ToString("yyMMdd"), 3);
            }
            txtWorkOrderNo.Attributes.Add("readonly", "true");
            txtScheduleID.Attributes.Add("readonly", "true");
            txtProductID.Attributes.Add("readonly", "true");
            txtProductName.Attributes.Add("readonly", "true");
            txtUnit.Attributes.Add("readonly", "true");
            txtProcessName.Attributes.Add("readonly", "true");
            cpBillDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void InitDDL()
        {
            BindParameterForDDL("SceneStock", ddlSceneStock);
            UIHelper.InsertFirstItem(ddlSceneStock);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                return;
            }

            BsHalftransferEntity entity = new BsHalftransferEntity();
            entity.HalfTransferID = GetString(lblHalfTransferID);
            entity.BillDate = GetDateTime(cpBillDate);
            entity.ScheduleDetailID = GetDecimal(hdScheduleDetailID);
            entity.WorkOrderNo = GetString(txtWorkOrderNo);
            entity.ScheduleProcessID = GetDecimal(hdScheduleProcessID);
            entity.SceneStock = GetString(ddlSceneStock);
            entity.ActulProduceNum = GetDecimal(txtActulProduceNum);
            entity.Status = "1";
            entity.CreateUserID = CurrentUser.UserID;
            entity.CreateUserName = CurrentUser.UserName;
            entity.CreateDate = DateTime.Now;
            entity.ModifyUserID = CurrentUser.UserID;
            entity.ModifyUserName = CurrentUser.UserName;
            entity.ModifyDate = DateTime.Now;


            BsHalfTransferBusiness.SaveOrUpdate(entity, "Save");
            base.LogAction(BaseEnum.ActionType.Add, entity.HalfTransferID, true, null, new EntityBase[] { entity });
            ClsComm.SetMaxSequenceID(m_strTbl);
            UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../StockManage/HalfTransferList.aspx");
        }

        private bool ValidateData()
        {
            string strMention = "";


            if (ClsComm.CheckExist("BS_HalfTransfer", "HalfTransferID", lblHalfTransferID.Text.Trim(), true))
            {
                strMention = "半成品流转单编号已经存在！<br />";
            }

            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                ClsComm.SetMaxSequenceID(m_strTbl);
                return false;
            }
        }

    }
}